﻿@inject NavigationManager _NavigationManager;
@inject IMessageService _Msg;
@inject HttpClient _HttpClient;
@implements IPageFlush
@page "/DetailInfo"
@using EmployeeLeaveSystem.Shared.Models;
<PageHeader Class="site-page-header" Title="请假单" Subtitle="详细信息" OnBack="@(()=> { App._appHandle?.BackPage(); })" />
<Title>@leaveDetail.Name</Title>
<Paragraph>
    <Title Level="3">明细</Title>
    <Descriptions Title="" Bordered>
        <DescriptionsItem Title="申请人">@leaveDetail.Staff?.Name</DescriptionsItem>
        <DescriptionsItem Title="发出日期">@leaveDetail.LeaveDate.ToString()</DescriptionsItem>
        <DescriptionsItem Title="申请日期">@leaveDetail.StartTime.ToString() 👉 @leaveDetail.EndTime.ToString()</DescriptionsItem>
        <DescriptionsItem Title="状态">@leaveDetail.AcceptFlag</DescriptionsItem>
        <DescriptionsItem Title="详细信息">@leaveDetail.Description</DescriptionsItem>
    </Descriptions>
    <Descriptions Title="" Bordered>
        <DescriptionsItem Title="确认人">@leaveDetail.AcceptStaff?.Name</DescriptionsItem>
        <DescriptionsItem Title="确认标志">@leaveDetail.AcceptFlag</DescriptionsItem>
    </Descriptions>
</Paragraph>
<Paragraph>
    @{
        var lastTime = (DateTime.Now - leaveDetail.EndTime).Days;
        var totalTime = (leaveDetail.EndTime - leaveDetail.StartTime).Days;
        int days = 0;
        string msg = "";
        ProgressStatus statues = ProgressStatus.Normal;
        double precent = 0;
        if (DateTime.Now < leaveDetail.StartTime) {
            msg = "尚未进入假期";
            days = (leaveDetail.StartTime - DateTime.Now).Days;
            statues = ProgressStatus.Exception;
            precent = 100;
        } else if (DateTime.Now >= leaveDetail.StartTime && DateTime.Now <= leaveDetail.EndTime) {
            msg = "距离请假结束还有";
            days = (leaveDetail.EndTime - DateTime.Now).Days;
            statues = ProgressStatus.Normal;
            precent = days * 1.0d / totalTime * 100.0d;
            var st = precent.ToString("f2");
            double.TryParse(st,out precent);
        } else if (DateTime.Now > leaveDetail.EndTime) {
            msg = "当前事件已经过了请假时间";
            days = (DateTime.Now - leaveDetail.EndTime).Days;
            precent = 100;
            statues = ProgressStatus.Success;
        }
    }
    <div style="display:flex;">
        <AntDesign.Progress Type="ProgressType.Circle" Percent="@precent" Status="@statues" />
        <div style="margin:10px;margin: 0px 0px 0 30px; display: flex; flex-direction: column; justify-content: center;">
            <Title Level="4">当前</Title>
            <div>@msg</div>
            <div style="font-size:30px">@days 天</div>
        </div>
    </div>
    <Divider />
    <Title Level="3">消息</Title>
    <p>有关所有的本请假条消息在此</p>
    @foreach (var it in leaveDetail.Comments) {
        string _back_col = "#ffffff";// 绿：#e4ffe4 红：#ffd1d1 白：#ffffff
        string? _msgComment = null;
        if (it.Type == CommentType.Accept) { _back_col = "e4ffe4"; _msgComment = "同意请假记录："; }
        if (it.Type == CommentType.Finished) { _back_col = "#e4ffe4"; _msgComment = "申请销假记录："; }
        if (it.Type == CommentType.Reject) { _back_col = "ffd1d1"; _msgComment = "拒绝申请记录："; }
        string _style_color_attribute = $"background-color: {_back_col}";
        if (_msgComment != null) {
            <p style="font-weight:bolder">@_msgComment</p>
        }
        <Comment Author="@it.StaffInfo?.Name"
             Avatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
             Content="@it.Msg"
             Datetime="@it.Time.ToString()"
             Style="@_style_color_attribute">
        </Comment>
    }
    <p></p>
    <Button OnClick="()=>{ App._appHandle?.PostMsgToIdLeaveDetail(leaveDetail.Id);}">添加新的回复</Button>
</Paragraph>

@code {
    public static int Id { get; set; } = 0;
    public LeaveDetail leaveDetail = new();
    public static void NavTo(int id) {
        LeaveDetailsInfo.Id = id;
        App._appHandle?.ToUrl("/DetailInfo");
    }
    protected override void OnInitialized() {
        GobalManager.AddPage(this);
        base.OnInitialized();
    }
    protected async override Task OnInitializedAsync() {
        try {
            // 查询 请假记录表格
            var res = await _HttpClient.GetFromJsonAsync<LeaveDetail>($"Main/GetDetaisById?id={Id}");
            if (res != null) leaveDetail = res; else App._appHandle?.NoticeWithIcon(NotificationType.Error, "网络", "获取消息状态失败");
            // 查询 Comments 列表
            var res2 = await _HttpClient.GetFromJsonAsync<List<Comments>>($"Main/GetCommentsWithStaffInfoByLeavesId?id={Id}");
            leaveDetail.Comments = res2;
            // 查询 Accept用户 和 申请人用户 信息
            var res3 = await _HttpClient.GetFromJsonAsync<StaffInfo>($"Main/GetStaffInfoById?id={leaveDetail.AcceptStaffId}");
            leaveDetail.AcceptStaff = res3;
            var res4 = await _HttpClient.GetFromJsonAsync<StaffInfo>($"Main/GetStaffInfoById?id={leaveDetail.StaffId}");
            leaveDetail.Staff = res4;
        }
        catch (Exception ex) {
            App._appHandle?.NoticeWithIcon(NotificationType.Error, "程序出错", ex.Message);
        }
    }
    public async void Flush() {
        await OnInitializedAsync();
        StateHasChanged();
    }
}
